package Solutions

import "unsafe"

func addStrings(num1 string, num2 string) string {
	l1, l2 := len(num1)-1, len(num2)-1
	if l1 < l2 {
		l1, l2 = l2, l1
		num1, num2 = num2, num1
	}
	ans := make([]byte, 0, l1+1)
	var flag byte = 0
	for l2 >= 0 {
		var sum byte
		flag, sum = addChar(num1[l1], num2[l2], flag)
		ans = append(ans, sum)
		l1--
		l2--
	}
	for l1 >= 0 {
		var sum byte
		flag, sum = addChar(num1[l1], '0', flag)
		ans = append(ans, sum)
		l1--
	}
	if flag == 1 {
		ans = append(ans, '1')
	}
	for l, r := 0, len(ans)-1; l < r; l, r = l+1, r-1 {
		ans[l], ans[r] = ans[r], ans[l]
	}
	return *(*string)(unsafe.Pointer(&struct {
		Date uintptr
		Len  int
	}{
		uintptr(unsafe.Pointer(&ans[0])),
		len(ans),
	}))
}

func addChar(a, b byte, flag byte) (byte, byte) {
	c := a + b - '0' + flag
	if c > '9' {
		return 1, c - 10
	}
	return 0, c
}
